package interfaces;

import fileHandling.language.Lang;
import interfaces.language.LanguageLabel;
import interfaces.superWidgets.StaticContent;

import org.fenggui.binding.render.ImageFont;
import org.fenggui.decorator.background.PlainBackground;
import org.fenggui.util.Color;

public class PropertyContent extends StaticContent {
	
	public enum Mode { UP, LEFT }
	
	private StaticContent maxValueContent, valueContent, upgradeContent;
	private float value, maxValue, upgradeValue;
	private int border, pieceWidth, pieceHeight, contentX;
	protected LanguageLabel descriptionLabel;
	private boolean isShowingUpgrade;
	private Mode mode;
	
	public PropertyContent(int width, int height, int x, int y) {
		super(width, height, x, y);
		
		init(null);		
		initValueContents();
	}
	
	public PropertyContent(int width, int height, int x, int y, Lang description, Mode mode) {
		super(width, height, x, y);
		this.mode = mode;
		
		init(description);		
		initValueContents();
	}
	
	private void init(Lang description) {
		isShowingUpgrade = false;
		
		pieceWidth = width;
		pieceHeight = height;
		contentX = 0;
		border = height / 6;
		
		if(mode != null) {
			int labelY = 0;
			switch(mode) {
				case LEFT : pieceWidth = width / 2;
							pieceHeight = height;
							contentX = pieceWidth;
							break;
				case UP : pieceWidth = width;
						  pieceHeight = height / 2;
						  contentX = 0;
						  labelY = pieceHeight;
			}
			
			if(description != null) {
				descriptionLabel = new LanguageLabel(description, GUISource.middleFont);
				descriptionLabel.setSize(pieceWidth, pieceHeight);
				descriptionLabel.setXY(0, labelY);
				addWidget(descriptionLabel);
			}
		}
	}
	
	private void initValueContents() {
		maxValueContent = new StaticContent(pieceWidth, pieceHeight, contentX, 0);
		maxValueContent.getAppearance().add(new PlainBackground(Color.BLACK));
		addWidget(maxValueContent);
		
		int valueHeight = pieceHeight - border;
		valueContent = new StaticContent(0, valueHeight, contentX + (border / 2), border / 2);
		updateValue();
		valueContent.getAppearance().add(new PlainBackground(Color.BLUE));
		addWidget(valueContent);
		
		upgradeContent = new StaticContent(0, valueHeight, contentX + valueContent.getWidth(), valueContent.getY());
		updateUpgradeValue();
		upgradeContent.getAppearance().add(new PlainBackground(Color.GREEN));
	}
	
	private void updateValue() {
		checkValues();
		int valueWidth = (int)((pieceWidth - border) * getValue() / getMaxValue());
		valueContent.setWidth(valueWidth);
	}
	
	private void updateUpgradeValue() {
		checkValues();
		int valueWidth = (int)((pieceWidth - border) * getUpgradeValue() / getMaxValue());
		upgradeContent.setWidth(valueWidth);
		upgradeContent.setX(valueContent.getWidth() + valueContent.getX());
	}
	
	public void showUpgrade(boolean show) {
		if(show && !isShowingUpgrade) addWidget(upgradeContent);
		else if(!show && isShowingUpgrade) removeWidget(upgradeContent);
		isShowingUpgrade = show;
		layout();
	}
	
	public void setMaxValue(float newMaxValue) {
		maxValue = newMaxValue;
		if(value > maxValue && value != 0) value = maxValue;
		updateValue();
		updateUpgradeValue();
		layout();
	}
	
	public void setValue(float newValue) {
		value = newValue;
		if(value > maxValue && maxValue != 0) value = maxValue;
		updateValue();
		updateUpgradeValue();
		layout();
	}
	
	public void setUpgradeValue(float newUpgradeValue) {
		upgradeValue = newUpgradeValue;
		updateValue();
		updateUpgradeValue();
		layout();
	}
	
	private void checkValues() {
		if(value < 0f) value = 0f;
		if(maxValue < 0f) maxValue = 0f;
		if(upgradeValue < 0f) upgradeValue = 0f;
	}
	
	public void changeBackgroundColor(Color color) {
		changeColor(maxValueContent, color);
	}
	
	public void changeUpgradeColor(Color color) {
		changeColor(upgradeContent, color);
	}
	
	public void changeValueColor(Color color) {
		changeColor(valueContent, color);
	}
	
	private void changeColor(StaticContent content, Color color) {
		content.getAppearance().removeAll();
		content.getAppearance().add(new PlainBackground(color));
	}
	
	public void changeTextColor(Color color) {
		GUISource.setTextColor(descriptionLabel, color);
	}
	
	public float getMaxValue() {
		if(maxValue == 0) return 0.1f;
		return maxValue;
	}
	
	public float getValue() { return value; }
	
	public float getUpgradeValue() { return upgradeValue; }
	
	public void setText(Lang text) {
		descriptionLabel.setLangText(text);
	}
	
	@Override
	public void setSize(int width, int height) {
		super.setSize(width, height);
		init(null);
		
		if(maxValueContent != null) maxValueContent.setSize(pieceWidth, pieceHeight);
		if(valueContent != null) {
			updateValue();
			valueContent.setHeight(pieceHeight - border);
			valueContent.setXY(contentX + (border / 2), border / 2);
		}
		if(upgradeContent != null) {
			updateUpgradeValue();
			upgradeContent.setHeight(pieceHeight - border);
			upgradeContent.setY(border / 2);
		}
		if(descriptionLabel != null) {
			descriptionLabel.setSize(pieceWidth, pieceHeight);
			if(mode == Mode.UP) descriptionLabel.setY(pieceHeight);
		}
		layout();
	}
	
	public void setTextFont(ImageFont font) { GUISource.setFont(descriptionLabel, font); }
	
	public boolean isShowingUpgrade() { return isShowingUpgrade; }
}